package com.framework.foundation.weixin.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.framework.foundation.weixin.wxdevice.Wxdevice;
import com.framework.foundation.weixin.wxmac.Wxmac;
import com.framework.foundation.weixin.wxstore.Wxstore;
import com.jfinal.kit.HttpKit;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;

public class WifiApi {

	private static final Log LOG = Log.getLog(WifiApi.class);

	/**
	 * 查询设备列表
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月17日 下午2:09:18
	 */
	public static ApiResult getDeviceList(int pageindex, int shopId) {
		String url = "https://api.weixin.qq.com/bizwifi/device/list?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("pageindex", (pageindex < 1) ? 1 : pageindex);
		data.put("pagesize", 20);// 默认每页显示20条记录
		if (shopId > 0) {
			data.put("shop_id", shopId);
		}
		data.put("ssid", "WX-JZZ5");
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 查询商家主页
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月17日 下午2:09:18
	 */
	public static ApiResult getHomepage(int shopId) {
		String url = "https://api.weixin.qq.com/bizwifi/homepage/get?access_token="
				+ AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_id", shopId);
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 获取门店wifi信息
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月17日 下午2:09:18
	 */
	public static ApiResult getWifiInfo(int shopId) {
		String url = "https://api.weixin.qq.com/bizwifi/shop/get?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_id", shopId);
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 修改设备
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月18日 上午10:17:42
	 */
	public static ApiResult updateDevice(Wxdevice oldDevice, Wxdevice newDevice) {
		String url = "https://api.weixin.qq.com/bizwifi/shop/update?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_id", newDevice.get("shop_id"));
		data.put("old_ssid", oldDevice.get("ssid"));
		data.put("ssid", newDevice.get("ssid"));
		data.put("password", newDevice.get("password"));
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 设置商家主页
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月18日 上午10:17:42
	 */
	public static ApiResult updateStoreHomePage(Wxstore store) {
		String url = "https://api.weixin.qq.com/bizwifi/homepage/set?access_token="
				+ AccessTokenApi.getAccessTokenStr();
		Map<String, Object> struct = new HashMap<String, Object>();
		struct.put("url", store.get("homepage"));

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_id", store.get("shop_id"));
		data.put("template_id", 1);
		data.put("struct", struct);
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 设置顶部常驻入口文案
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月18日 上午10:17:42
	 */
	public static ApiResult updateStoreBar(Wxstore store) {
		String url = "https://api.weixin.qq.com/bizwifi/bar/set?access_token=" + AccessTokenApi.getAccessTokenStr();

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_id", store.get("shop_id"));
		data.put("bar_type", store.getInt("bar_type"));
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 获取门店列表
	 * 
	 * @return ApiResult
	 */
	public static ApiResult getStoreList(int begin) {
		String url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token="
				+ AccessTokenApi.getAccessTokenStr();
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("begin", begin);
		mapData.put("limit", 20);
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 获取所有门店列表
	 * 
	 * @return ApiResult
	 */
	public static List<Map<String, Object>> getAllStoreList() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		int begin = 0;
		ApiResult apiResult = WifiApi.getStoreList(begin);
		while ((apiResult != null) && apiResult.isSucceed()) {
			int totalCount = apiResult.getInt("total_count");
			List<Map<String, Object>> businessList = apiResult.getList("business_list");
			if (CollectionUtils.isNotEmpty(businessList)) {
				list.addAll(businessList);
			}
			if ((begin + 20) < totalCount) {
				apiResult = WifiApi.getStoreList(begin += 20);
			} else {
				apiResult = null;
			}
		}
		return list;
	}

	/**
	 * 删除mac地址
	 * 
	 * @return ApiResult
	 */
	public static ApiResult deleteMac(Wxmac mac) {
		String url = "https://api.weixin.qq.com/bizwifi/device/delete?access_token="
				+ AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("bssid", mac.getStr("address"));
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 清空门店网络及设备
	 * 
	 * @return ApiResult
	 */
	public static ApiResult deleteDevice(Wxdevice device) {
		String url = "https://api.weixin.qq.com/bizwifi/shop/clean?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_id", device.getInt("shop_id"));
		data.put("ssid", device.getStr("ssid"));
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 清空门店所有网络及设备
	 * 
	 * @return ApiResult
	 */
	public static ApiResult deleteAllDevice(Wxstore store) {
		String url = "https://api.weixin.qq.com/bizwifi/shop/clean?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("shop_id", store.getInt("shop_id"));
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 获取设备信息
	 * 
	 * @return ApiResult
	 */
	public static ApiResult getDeviceInfo(int pageindex, Integer shopId) {
		String url = "https://api.weixin.qq.com/bizwifi/device/list?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("pageindex", pageindex);
		data.put("pagesize", 20);
		if (shopId != null) {
			data.put("shop_id", shopId);
		}
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 获取所有设备信息
	 * 
	 * @return ApiResult
	 */
	public static List<Map<String, Object>> getAllDeviceInfo(Integer shopId) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		int pageindex = 1;
		ApiResult apiResult = WifiApi.getDeviceInfo(pageindex, shopId);
		while ((apiResult != null) && apiResult.isSucceed()) {
			Map data = apiResult.getMap("data");
			int pagecount = (Integer) data.get("pagecount");
			List<Map<String, Object>> records = (List) data.get("records");
			if (CollectionUtils.isNotEmpty(records)) {
				list.addAll(records);
			}
			if (pageindex < pagecount) {
				apiResult = WifiApi.getDeviceInfo(pageindex++, shopId);
			} else {
				apiResult = null;
			}
		}
		return list;
	}

	/**
	 * 获取类目列表
	 * 
	 * @return ApiResult
	 */
	public static ApiResult getCategoryList(int bengin) {
		String url = "http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token="
				+ AccessTokenApi.getAccessTokenStr();
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("begin", bengin);
		mapData.put("limit", 20);
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 获取wifi门店列表
	 * 
	 * @return ApiResult
	 */
	public static ApiResult getWifiStoreList(int pageindex) {
		String url = "https://api.weixin.qq.com/bizwifi/shop/list?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("pageindex", (pageindex < 1) ? 1 : pageindex);
		data.put("pagesize", 20);// 默认每页显示20条记录
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(data));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 递归获取所有wifi门店信息
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月18日 下午3:40:08
	 */
	public static List<Map<String, Object>> getAllWifiStoreList() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		int pageindex = 1;
		ApiResult apiResult = WifiApi.getWifiStoreList(pageindex);
		while ((apiResult != null) && apiResult.isSucceed()) {
			Map data = apiResult.getMap("data");
			int pagecount = (Integer) data.get("pagecount");
			List<Map<String, Object>> records = (List) data.get("records");
			if (CollectionUtils.isNotEmpty(records)) {
				list.addAll(records);
			}
			if (pageindex < pagecount) {
				apiResult = WifiApi.getWifiStoreList(pageindex++);
			} else {
				apiResult = null;
			}
		}
		return list;
	}

	/**
	 * 获取wifi物料二维码
	 * 
	 * @return ApiResult
	 */
	public static ApiResult getWifiQrcode(int shopId, String ssid, int imgId) {
		String url = "https://api.weixin.qq.com/bizwifi/qrcode/get?access_token=" + AccessTokenApi.getAccessTokenStr();

		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("shop_id", shopId);
		dataMap.put("img_id", imgId);
		if (StringUtils.isNotBlank(ssid)) {
			dataMap.put("ssid", ssid);
		}
		String jsonResult = HttpUtils.post(url, JsonUtils.toJson(dataMap));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 
	 * @return
	 * @Author: Brin
	 * @Date: 2016年3月14日 下午2:55:56
	 */
	public static ApiResult addStore(Wxstore store) {
		String url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Map<String, Object>> groupData = new HashMap<String, Map<String, Object>>();
		Map<String, Object> data = new LinkedHashMap<String, Object>();
		data.put("sid", store.getInt("id").toString());
		data.put("business_name", store.getStr("business_name"));
		data.put("province", store.getStr("province"));
		data.put("city", store.getStr("city"));
		data.put("district", store.getStr("district"));
		data.put("address", store.getStr("address"));
		data.put("telephone", store.getStr("telephone"));
		data.put("categories", new String[] { store.getStr("categories") });
		data.put("offset_type", store.getInt("offset_type"));
		data.put("longitude", store.getDouble("longitude"));
		data.put("latitude", store.getDouble("latitude"));

		Map<String, Object> baseinfoData = new HashMap<String, Object>();
		baseinfoData.put("base_info", data);

		Map<String, Object> businessData = new HashMap<String, Object>();
		businessData.put("business", baseinfoData);
		System.out.println(JsonUtils.toJson(businessData));
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(businessData));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}

	/**
	 * 添加密码型设备
	 * 
	 * @return ApiResult
	 */
	public static ApiResult addDevice(Wxdevice device) {
		String url = "https://api.weixin.qq.com/bizwifi/device/add?access_token=" + AccessTokenApi.getAccessTokenStr();
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("shop_id", device.getInt("shop_id"));
		mapData.put("ssid", device.getStr("ssid"));
		mapData.put("password", device.getStr("password"));
		String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
		LOG.info(jsonResult);
		return new ApiResult(jsonResult);
	}
}
